package com.itheima.config;


import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@ComponentScan(value = "com.itheima", excludeFilters =
        {
                //排除一个就写一个，多少个写多少个
                //        @AliasFor("classes")    ——————要写class
                //        Class<?>[] value() default {};
                //
                //        @AliasFor("value")
                //        Class<?>[] classes() default {};
                @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class)
        }
/*   源码中需要这样的一个数组： ComponentScan.Filter[] excludeFilters() default {};*/

)
@EnableAspectJAutoProxy//开启aop支持
@EnableTransactionManagement  //开启事务注解驱动
public class SpringConfig {
    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        // <!--目的是给一个name为DataSource的传一个dataSource-->

        return dataSourceTransactionManager;

    }

    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource) {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.itheima.pojo");
        return factoryBean;
    }

    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage("com.itheima.dao");
        return configurer;
    }
}
